import { get, post, message } from '@/utils'
import type { ProColumns } from '../../packages/pro-components/components/pro-table'
import GroupBtn from '@/components/common/GroupBtn.vue'
export interface MyProColumns extends ProColumns {
  btnList?: BtnItem[] | ((any) => BtnItem[])
  valueSearch?: boolean //是否开启远程搜索
  api?: string //请求地址
  filedName?: { label: string; value: string } //转换数据成option
  renderType?: DiyConfigRenderType
  /**隐藏在弹窗 */
  hideInModal?: boolean
  modelFormItemProps?: any
}
const useTable = () => {
  /**
   *
   * @param columns
   * @param defaultOptions 列的默认配置项
   * @returns
   */
  const formatColumns = (columns: MyProColumns[], defaultOptions?: Partial<MyProColumns>) => {
    const defaultOption: MyProColumns = {
      align: 'center',
      ellipsis: false,
      tooltip: true,
      dataIndex: '',
      title: '',
      hideInSearch: true, //是否显示在搜索栏
    }
    const processColumns = (columns: MyProColumns[]) => {
      const processedColumns: MyProColumns[] = []
      const processColumn = (column: MyProColumns) => {
        const processedColumn: MyProColumns = {
          ...defaultOption,
          ...defaultOptions,
          ...column,
          slotName: column.dataIndex,
          formItemProps: {
            'label-col-props': { span: 10 },
            'wrapper-col-props': { span: 14 },
            ...column.formItemProps,
          },
        }

        if (column.children && column.children.length > 0) {
          processedColumn.children = column.children.map(child => {
            const processedChild: MyProColumns = {
              ...defaultOption,
              ...defaultOptions,
              ...child,
              slotName: child.dataIndex,
            }
            return processColumn(processedChild)
          })
        }

        return processedColumn
      }

      columns.forEach(column => {
        const processed = processColumn(column)
        if (Array.isArray(processed.btnList)) {
          processed.render = ({ record }) => h(GroupBtn, { record, list: processed.btnList as BtnItem[] })
        }
        processedColumns.push(processed)
      })

      return processedColumns
    }
    return processColumns(columns)
  }
  return { formatColumns }
}

export default useTable
